package com.firefightingiot.firefightingiot.web.service;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import com.firefightingiot.firefightingiot.common.base.BaseService;
import com.firefightingiot.firefightingiot.model.IpBroadcastFile;
import com.firefightingiot.firefightingiot.support.model.RespEntity;
import com.firefightingiot.firefightingiot.support.mybatisplus.QueryWrapper;
import com.firefightingiot.firefightingiot.web.mapper.IpBroadcastFileMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: YangYu
 * @ClassName: IPBroadCastAsyncService
 * @Date: Created in 10:14 2022/3/25
 * @Description: IP广播异步服务
 */
@Service
public class IPBroadCastAsyncService extends BaseService {

    @Resource
    IpBroadcastFileMapper ipBroadcastFileMapper;



    /**
     * @Author YangYu
     * @Description 异步更新文件
     * @Date Created in 12:03 2022/3/25
     * @Param [jsonParam]
     */
    public RespEntity updateServerMusicId(JSONObject jsonParam){
        //获取全部文件
        List<IpBroadcastFile> fileList = ipBroadcastFileMapper.selectList(new QueryWrapper<IpBroadcastFile>().eq("home_id",jsonParam.getStr("homeId")));
        //获取服务器上全部文件名
        JSONObject allMusicsInfo = IPBroadCast(jsonParam,"IPBroadcast","getAllMusicsInfo","IPBroadcast","GET");
        JSONArray musicInfoArray = allMusicsInfo.getJSONArray("musicinfoarray");
        //更新数据
        List<IpBroadcastFile> updateFileList = new ArrayList<>();
        for (IpBroadcastFile file : fileList){
            for (Object object : musicInfoArray){
                JSONObject jsonObject = (JSONObject)object;
                if (file.getFileName().equals(jsonObject.getStr("cpfilename"))){
                    if (!file.getServerMusicId().equals(jsonObject.getStr("servermusicid"))){
                        file.setServerMusicId(jsonObject.getStr("servermusicid"));
                        updateFileList.add(file);
                    }
                }
            }
        }
        ipBroadcastFileMapper.batchUpdate(updateFileList);
        return RespEntity.success();
    }

    public static void main(String[] args) {

    }
}
